<?php /**
* KT's Skraper
* Simple and easy to use web-scraping functionality for automation purposes.
*
* @link $URL: http://skraper.googlecode.com/svn/trunk/skraper.php $
* @version: $Id: skraper.php 10 2013-06-29 15:58:38Z kaloyan@gmail.com $
*/

/**
*
*/
Abstract Class skraper {

	/**
	* @var string name of the folder where to store the files
	*/
	Protected $archive = 'archive';

	/**
	* @var string URL for looking into what to download
	*/
	Protected $url;

	/**
	* @var string User-agent for the Skraper to identify
	*/
	Protected $user_agent = 'SKRAPER <http://skraper.googlecode.com/>';

	/**
	* Constructor: checks the settings, then starts the whole process
	*/
	Public Function __construct() {

		if (empty($this->archive)) {
			$this->archive = get_class($this);
			}

		if (!file_exists($this->archive)) {
			mkdir($this->archive, 0777);
			}
		
		if (!is_readable($this->archive)) {
			throw new Exception(
				"The archive folder \"{$this->archive}\" is not readable");
			}
		
		if (!is_dir($this->archive)) {
			throw new Exception(
				"The archive folder \"{$this->archive}\" is not a directory");
			}
		
		if (!$html = $this->download($this->url)) {
			throw new Exception(
				"Not able to download \"{$this->url}\" page");
			}
			
		if (!$result = $this->parse($html)) {
			throw new Exception(
				"Not able to parse the HTML: {$html}");
			}
		
		$this->save($result);
		}

	/**
	* Shortcut method for fetching an URL
	* @param string $url
	* @return string the fetched HTML
	*/
	Protected Function download($url) {

		$ch = curl_init();

		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		//curl_setopt($ch, CURLINFO_HEADER_OUT, true);
		curl_setopt($ch, CURLOPT_ENCODING , 'gzip');
		
		curl_setopt($ch, CURLOPT_URL, $url);

		$u = parse_url($url);
		$cookie_txt = '/tmp/' . strToUpper($u['host']) . '.txt';
		curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_txt);
		curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_txt);

		curl_setopt($ch, CURLOPT_USERAGENT, $this->user_agent);

		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		//curl_setopt($ch, CURLOPT_VERBOSE, 1);
		//curl_setopt($ch, CURLOPT_HEADER, 1);

		$response = curl_exec($ch);
 
		if (false === $response) {
			throw new Exception(curl_error($ch) . ' (' . curl_errno($ch) . ')');
			}
			
		if (200 != ($code = curl_getinfo($ch, CURLINFO_HTTP_CODE))) {
			throw new Exception("Response code is not 200, but {$code}; " 
				. curl_getinfo($ch, CURLINFO_HEADER_OUT));
			}

		curl_close($ch);	
		
		return $response;
		}

	/** 
	*/
	Abstract Protected Function parse($html);

	/**
	*/
	Abstract Protected Function save(array $result);

	////--end-of-class----
	}
